package com.fxu.gen.cloud;

import cn.hutool.core.util.StrUtil;
import com.fxu.framework.core.base.Constant;
import com.fxu.framework.core.base.Result;
import com.fxu.gen.AutoUtil;
import com.fxu.gen.Base;
import com.fxu.gen.StrBuf;
import com.fxu.gen.enums.SignEnum;
import com.fxu.gen.enums.TplEnum;
import com.fxu.tpl.entity.Column;
import com.fxu.tpl.entity.Table;
import com.fxu.tpl.enums.ProjectTypeEnum;

import java.io.File;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * RestImpl类的模板替换
 * @author fangxu
 */
public final class RestImpl extends Base {
	public RestImpl(Table table, File destFile) {
		super(table, TplEnum.RestImpl, destFile);
	}
	
	@Override
    public String doReplace() {
		if (Objects.equals(project.getType(), ProjectTypeEnum.SPRING_BOOT.code())) {
			fileTxt = fileTxt.replace("import com.fxu.tpl.facade.feign.ApiCloudFeign;", "import "+corePackage+".base.cloud.ApiBaseServiceImpl;");
			fileTxt = fileTxt.replace("private ApiCloudFeign apiCloudFeign;", "private ApiBaseServiceImpl api"+StrUtil.upperFirst(table.getModule().getName())+"Feign;");
		}
		String fileTxt = super.doReplace();
		fileTxt = replace(fileTxt, SignEnum.ClassAuto, getSaveBefore().append(getFillBeans()));
		fileTxt = replaceImports(fileTxt, false); //放最后才能收集imports
		return fileTxt;
	}
	
	private StringBuffer getSaveBefore() {
		StrBuf buffer = new StrBuf();
		String newTxt = fileTxt;
		newTxt = replace(newTxt, SignEnum.ClassAuto, new StringBuffer());
		String mtd = "public Result<" + tableBean + "> saveBefore(" + tableBean + " dbBean, " + tableBean + " bean) {";
		if (!newTxt.contains(mtd)) {
			buffer.append(1, "@Override");
			buffer.append(1, mtd);
			//buffer.append(2, "if (bean != null) {");
			buffer.append(2, "if (dbBean == null) { // 添加");
			String priKeyName = getPriKeyClnName(columns);
			for (Column column : columns) {
				if (column.getName().equals(priKeyName)) {
					continue;
				}
				String limits, defVal = null;
				if (!StrUtil.isEmpty(column.getLimits())) {
					limits = column.getLimits();
					// 默认第一个
					if (limits.contains(":")) {
						defVal = limits.substring(0, limits.indexOf(":"));
					} else if (limits.contains(",")) {
						defVal = limits.substring(0, limits.indexOf(","));
					}
				}
				if (column.isString()) {
					defVal = "\"" + (defVal == null ? "" : defVal) + "\"";
				}
				// 数据库not null, 前端添加允许null, 那么必须在业务逻辑初始化
				if (isTrue(column.getNotNull()) && !isTrue(column.getRequired()) && StrUtil.isEmpty(column.getDefVal())
						&& !column.getName().equals(Constant.CLN_VERSION)
						&& !column.getName().equals(Constant.CLN_STATUS)) {
					String tmpDefVal = defVal == null ? getDefVal(column) : defVal;
					AutoUtil.toImports(imports, column.isDecimal() ? BigDecimal.class : null);
					AutoUtil.toImports(imports, column.isDate() ? Date.class : null);
					AutoUtil.toImports(imports, column.isFloat() ? Float.class : null);
					AutoUtil.toImports(imports, column.isDouble() ? Double.class : null);
					buffer.append(3, "if (bean.get{1}() == null) {", AutoUtil.getBean(column.getName()));
					buffer.append(4, "bean.set{1}({2});", AutoUtil.getBean(column.getName()), tmpDefVal);
					buffer.append(3, "}");
				}
			}
			buffer.append(2, "} else { // 更新");
			// buffer.append(2, "}");
			buffer.append(2, "}");
			buffer.append(2, "return Result.ok(bean);");
			buffer.append(1, "}");
			
			AutoUtil.toImports(imports, Result.class);
		}
		return buffer.toBuffer();
	}
	
	private String getFillBean() {
		StrBuf buffer = new StrBuf();
		String newTxt = fileTxt;
		newTxt = replace(newTxt, SignEnum.RestServiceImplAuto, new StringBuffer());
		String mtd = "public void fillBean(" + tableBean + " bean, List<String> showFlags) {";
		if (!newTxt.contains(mtd)) {
			AutoUtil.toImports(imports, List.class); //, "com.learned.core.util.MapUtil"

			buffer.append(1, "");
			buffer.append(1, "@Override");
			buffer.append(1, mtd);
			buffer.append(2, "return Result.ok(bean);");
			buffer.append(1, "}");
		}
		return buffer.toString();
	}

	private String getFillBeans() {
		StrBuf buffer = new StrBuf();
		String newTxt = fileTxt;
		newTxt = replace(newTxt, SignEnum.RestServiceImplAuto, new StringBuffer());
		String mtd = "public void fillBeans(List<" + tableBean + "> beans, List<String> showFlags) {";
		if (!newTxt.contains(mtd)) {
			AutoUtil.toImports(imports, List.class);

			buffer.append(1, "");
			buffer.append(1, "@Override");
			buffer.append(1, mtd);
			buffer.append(2, "// 按需要填充数据");
			buffer.append(1, "}");
		}
		return buffer.toString();
	}
	
	private static String getDefVal(Column column) {
		if (column == null) {
			return "";
		}
		if (column.isString()) {
			return "\"\"";
		} else if (column.isDate()) {
			return "new Date()";
		} else if (column.isLong()) {
			return "0L";
		} else if (column.isInteger()) {
        	return "0";
        } else if (column.isDecimal()) {
        	return "BigDecimal.ZERO";
        } else if (column.isBoolean()) {
        	return "false";
        } else if (column.isFloat()) {
        	return "0F";
        } else if (column.isDouble()) {
        	return "0D";
        } else {
        	System.out.println("RestImpl.getDefVal["+column.getName()+"]===>找不到类型!" + column);
        }
		return "";
    }
}
